package cn.edu.bjut.homework.chapter7;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

public class GridBagLayoutTester {
	private JFrame frm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GridBagLayoutTester window = new GridBagLayoutTester();
					window.frm.setVisible(true);
					window.frm.pack();
					window.frm.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GridBagLayoutTester() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frm = new JFrame();
		frm.setTitle("GridBagLayout布局测试");
		frm.setBounds(100, 100, 537, 383);
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frm.getContentPane().setLayout(new GridBagLayout());
		
		GridBagConstraints constraints = new GridBagConstraints(); 
		constraints.fill = GridBagConstraints.BOTH; 
		
		JLabel affiliationLabel = new JLabel("单位：");
		frm.getContentPane().add(affiliationLabel, constraints);
		
		JTextField affiliationText = new JTextField(); 
		affiliationText.setColumns(30);
		constraints.gridx = 1; 
		constraints.gridwidth = 3; 
		constraints.weightx = 1; 
		frm.getContentPane().add(affiliationText, constraints);
		
		JLabel ageLabel = new JLabel("年龄：");
		constraints.gridx = 4; 
		constraints.gridwidth = 1; 
		constraints.weightx = 0; 
		frm.getContentPane().add(ageLabel, constraints);
		
		JTextField ageText = new JTextField(); 
		ageText.setColumns(5);
		constraints.gridx = 5; 
		frm.getContentPane().add(ageText, constraints);
		
		JButton queyButton = new JButton("查询"); 
		constraints.gridx = 6; 
		frm.getContentPane().add(queyButton, constraints);
		
		JLabel classLabel = new JLabel("类别："); 
		constraints.gridx = 0; 
		constraints.gridy = 1; 
		frm.getContentPane().add(classLabel, constraints);
		
		// 创建列表运动会项目
		DefaultListModel<String> listModel = new DefaultListModel<String>(); 
		listModel.addElement("100米");
		listModel.addElement("200米");
		listModel.addElement("1000米");
		listModel.addElement("跳远");
		listModel.addElement("跳高");
		listModel.addElement("铅球");
		JList<String> list = new JList<String>(listModel); 
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		JScrollPane scrollListPane = new JScrollPane(list); 
		scrollListPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		constraints.gridy = 2; 
		constraints.gridwidth = 2; 
		constraints.weighty = 1; 
		frm.getContentPane().add(scrollListPane, constraints);
		
		// 查询结果
		String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" };
		String[][] rowData = { { "高明", "男", "计算机学院", "100米，铅球", "" }, 
				{ "白雪", "女", "经管学院", "100米，200米", "" },
				{ "高大上", "男", "人文学院", "1000米", "" } };
		JTable table = new JTable(new DefaultTableModel(rowData, columnName));
		JScrollPane scrollTablePane = new JScrollPane(table);
		scrollTablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		constraints.gridx = 2; 
		constraints.gridy = 1; 
		constraints.gridwidth = 5; 
		constraints.gridheight = 2; 
		constraints.weightx = 1; 
		frm.getContentPane().add(scrollTablePane, constraints);
	}
}